﻿/*
 Copyright aswing.org, see the LICENCE.txt.
*/

import org.aswing.awml.AwmlNamespace;
import org.aswing.awml.component.ContainerParser;
import org.aswing.Component;
import org.aswing.JWindow;
import org.aswing.LayoutManager;

/**
 * Parses {@link org.aswing.JWindow} level elements.
 * 
 * @author Igor Sadovskiy
 */
class org.aswing.awml.component.WindowParser extends ContainerParser {
	
	private static var ATTR_ACTIVE:String = "active";
	private static var ATTR_MODAL:String = "modal";
	private static var ATTR_PACK:String = "pack";
	
	private static var ATTR_ON_WINDOW_ACTIVATED:String = "on-window-activated";
	private static var ATTR_ON_WINDOW_DEACTIVATED:String = "on-window-deactivated";
	private static var ATTR_ON_WINDOW_OPENED:String = "on-window-opened";
	private static var ATTR_ON_WINDOW_CLOSED:String = "on-window-closed";
	private static var ATTR_ON_WINDOW_CLOSING:String = "on-window-closing";
	private static var ATTR_ON_WINDOW_ICONIFIED:String = "on-window-iconified";
	private static var ATTR_ON_WINDOW_RESTORED:String = "on-window-restored";
	private static var ATTR_ON_WINDOW_MAXIMIZED:String = "on-window-maximized";
	
	/**
	 * Constructor.
	 */
	public function WindowParser(Void) {
		super();
	}
	
	public function parse(awml:XMLNode, window:JWindow, namespace:AwmlNamespace):JWindow {
		if (window == null) {
			window = new JWindow(getOwner(awml));	
		}
		
		super.parse(awml, window, namespace);
		
		// set active
		window.setActive(getAttributeAsBoolean(awml,  ATTR_ACTIVE, window.isActive()));
		
		// set modal
		window.setModal(getAttributeAsBoolean(awml, ATTR_MODAL, window.isModal()));
		
		// pack window
		if (getAttributeAsBoolean(awml, ATTR_PACK, false)) { 
			window.pack();
		}

        // init events
        attachEventListener(window, JWindow.ON_WINDOW_ACTIVATED, getAttributeAsEventListenerInfo(awml, ATTR_ON_WINDOW_ACTIVATED));
        attachEventListener(window, JWindow.ON_WINDOW_DEACTIVATED, getAttributeAsEventListenerInfo(awml, ATTR_ON_WINDOW_DEACTIVATED));
        attachEventListener(window, JWindow.ON_WINDOW_OPENED, getAttributeAsEventListenerInfo(awml, ATTR_ON_WINDOW_OPENED));
        attachEventListener(window, JWindow.ON_WINDOW_CLOSED, getAttributeAsEventListenerInfo(awml, ATTR_ON_WINDOW_CLOSED));
        attachEventListener(window, JWindow.ON_WINDOW_CLOSING, getAttributeAsEventListenerInfo(awml, ATTR_ON_WINDOW_CLOSING));
        attachEventListener(window, JWindow.ON_WINDOW_ICONIFIED, getAttributeAsEventListenerInfo(awml, ATTR_ON_WINDOW_ICONIFIED));
        attachEventListener(window, JWindow.ON_WINDOW_RESTORED, getAttributeAsEventListenerInfo(awml, ATTR_ON_WINDOW_RESTORED));
        attachEventListener(window, JWindow.ON_WINDOW_MAXIMIZED, getAttributeAsEventListenerInfo(awml, ATTR_ON_WINDOW_MAXIMIZED));
		
		return window;
	}

	/**
	 * Appends <code>component</code> to the <code>window</code>.
	 * 
	 * @param window the window to add the component to
	 * @param component the component to add to the window
	 */
	private function append(window:JWindow, component:Component):Void {
		window.getContentPane().append(component);
	}
	
	/**
	 * Set <code>layout</code> to the <code>window</code>.
	 * 
	 * @param window the window to set layout
	 * @param layout the layout to set 
	 */
	private function setLayout(window:JWindow, layout:LayoutManager):Void {
		window.getContentPane().setLayout(layout);
	}
	
}
